package com.adroiti.texasholdem.engine.combinations;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

import com.adroiti.texasholdem.Card;

public class Flush extends AbstractCombination {

	static class ColorComparator implements Comparator<Card> {

		@Override
		public int compare(Card o1, Card o2) {
			return o1.getColor().compareTo(o2.getColor());
		}

	}

	private High high;

	public Flush() {
		this(new High());
	}

	public Flush(High high) {
		this.high = high;
	}

	@Override
	public String getName() {
		return "Flush";
	}

	@Override
	public int getCombinationRank() {
		return 6;
	}

	protected int getRank(List<Card> playerCards, List<Card> tableCards, boolean checkStraight) {
		List<Card> cards = new LinkedList<Card>();
		cards.addAll(playerCards);
		cards.addAll(tableCards);

		Collections.sort(cards, new ColorComparator());

		int num = 0;

		for (int i = 0; i < cards.size() - 1; i++) {
			if (cards.get(i).getColor().equals(cards.get(i + 1).getColor())) {
				num++;
				if (num == 4) {
					return high.getRank(playerCards, tableCards);
				}
			} else
				num = 0;
		}

		return 0;
	}

	@Override
	public int getRank(List<Card> playerCards, List<Card> tableCards) {
		return getRank(playerCards, tableCards, false);
	}

}
